<template>
  <div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <h3 class="login_title">系统登录</h3>
      <el-form-item label="昵称或邮箱" prop="emailOrNickname">
        <el-input type="email" v-model="ruleForm.emailOrNickname" autocomplete="off" placeholder="请输入昵称或邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="checkPass" >
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="subAjax">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import qs from 'qs'
import router from '../router/index'
export default {
  name: 'Login',
  data () {
    var checkEmailOrNickname = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入昵称或邮箱'))
      } else {
        // if (this.ruleForm.emailOrNickname!== '') {
        //   this.$refs.ruleForm.validateField('emailOrNickname');
        // }
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        email: '',
        password: ''
      },
      rules: {
        email: [
          { validator: checkEmailOrNickname, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    subAjax () {
      // this.$axios.interceptors.request.use(function (config) {
      //   // console.log('config', config)    //这里可以打印一下看看里面内容
      //   let token = sessionStorage.getItem('token')
      //   if (token) {
      //     config.headers['Token'] = token
      //   }
      //   return config
      // }, function (error) {
      //   return Promise.reject(error)
      // })
      this.$axios({
        url: `user/login`,
        method: 'post',
        data: {
          email: this.ruleForm.emailOrNickname,
          password: this.ruleForm.checkPass
        }
      }).then(res => {
        // let isLogin = res.status // 这里接收状态码
        if (res.data.status === 100) { // 100可换
          console.log(res.data)
          if (res.data.data.flag === 200) {
            localStorage.setItem('token', res.data.data.msg)// 存到localStorage里面
            // alert(res.data.data.msg)
            router.replace({path: '/index'})
          } else if (res.data.data.flag === 250) {
            alert(res.data.data.msg)
            router.push({path: '/index'})
          } else {
            alert(res.data.data.msg)
          }
        }
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
.demo-ruleForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

</style>
